import React, { Component } from 'react'
import PropTypes from 'prop-types';
import './index.css'

export default class Footer extends Component {

    static propTypes = {
        todoList: PropTypes.array.isRequired,
        selectAll: PropTypes.func.isRequired
    }

    handleSelectAll = (event)=>{
        this.props.selectAll(event.target.checked);
    }

    handleDeleteAll = ()=>{
        this.props.deleteAllSelected();
    }

    render() {
        const {todoList} = this.props;
        const total = todoList.length;
        // 使用reduce 进行分类统计，获取已完成的任务个数
        const finishedCount = todoList.reduce((pre,todoObj)=>{
            return pre + (todoObj.finished ? 1 : 0)
        },0)
        return (
            <div className="todo-footer">
                <label>
                    {/* 
                        这里需要注意，当手动全选中时，需要自勾选
                        且此时任务列表中需要有任务
                    */}
                    <input checked={total === finishedCount && total!==0} onChange={this.handleSelectAll} type="checkbox" />
                </label>
                <span>
                    <span>已完成{finishedCount}</span> / 全部{total}
                        </span>
                <button onClick={this.handleDeleteAll} className="btn btn-danger">清除已完成任务</button>
            </div>
        )
    }
}
